package com.geeklan.smart.ui.feed.fragment.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.geeklan.smart.R;
import com.geeklan.smart.common.Constants;
import com.geeklan.smart.data.model.FeedRule;
import com.geeklan.smart.databinding.AdapterFeedingRuleItemBinding;
import com.geeklan.smart.widget.ItemOnClickListener;

import java.util.Arrays;
import java.util.List;

public class FeedingRuleAdapter extends RecyclerView.Adapter<FeedingRuleAdapter.ItemViewHolder> {
    private Context mContext;
    private List<FeedRule> itemList;
    private ItemOnClickListener itemOnClickListener;
    private DeleteItemListener deleteItemListener;

    public void setDeleteItemListener(DeleteItemListener deleteItemListener) {
        this.deleteItemListener = deleteItemListener;
    }

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public FeedingRuleAdapter(Context context, List<FeedRule> itemList) {
        this.itemList = itemList;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FeedingRuleAdapter.ItemViewHolder(AdapterFeedingRuleItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        FeedRule timedPlan = itemList.get(position);
        holder.mBinding.scIsOpen.setChecked(timedPlan.getIsOpen() == 1);
        holder.mBinding.tvFeedTime.setText(timedPlan.getStartTime());
        String repetitionPeriod = timedPlan.getRepetitionPeriod();
        StringBuilder stringBuilder = new StringBuilder("");
        if(!"0".equals(repetitionPeriod)){
            String[] weeks = repetitionPeriod.split(",");
            try {
                for (String string : weeks) {
                    int week = Integer.parseInt(string);
                    stringBuilder.append(Constants.WEEKDAYS[week-1]).append(" ");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            stringBuilder.append(mContext.getResources().getString(R.string.every_day));
        }

        List<String> feedType = Arrays.asList(mContext.getResources().getStringArray(R.array.feed_type));
        holder.mBinding.tvFeedNumber.setText(feedType.get(timedPlan.getFeedType()) + "*" + timedPlan.getFeedNum());

        holder.mBinding.tvFeedLoop.setText(stringBuilder.toString());

        holder.mBinding.getRoot().setOnClickListener(v -> {
            itemOnClickListener.OnItemClick(v,position);
        });
        holder.mBinding.ivDelete.setOnClickListener(v -> {
            deleteItemListener.onDelete(timedPlan,position);
        });
        holder.mBinding.scIsOpen.setOnClickListener(v -> {
            deleteItemListener.isOpen(holder.mBinding.scIsOpen.isChecked(),timedPlan,position);
        });
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        AdapterFeedingRuleItemBinding mBinding;

        public ItemViewHolder(@NonNull AdapterFeedingRuleItemBinding itemView) {
            super(itemView.getRoot());
            mBinding = itemView;
        }
    }

    public interface DeleteItemListener {
        void onDelete(FeedRule timedPlan, int position);
        void isOpen(boolean isOpen,FeedRule timedPlan, int position);
    }
}